package com.taobao.top.demo.getData;

import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import com.taobao.top.demo.common.DemoConstant;
import com.taobao.top.demo.common.Util;
import com.taobao.top.demo.dataobject.PropValue;
import com.taobao.top.demo.exception.DemoException;
/**
 * 调用 taobao.itempropvalues.get
 * 获取标准类目属性值
 * @author Jessica
 *
 */
public class GetItemPropValues {
	/**
	 * 组装请求参数
	 * @param cid 商品的类目id
	 * @return
	 */
	private static String createRequestParam(String cid) {
		TreeMap<String, String> apiparamsMap = new TreeMap<String, String>();
		// 组装协议参数。
		apiparamsMap.put("method", "taobao.itempropvalues.get");
		apiparamsMap.put("app_key", DemoConstant.APP_KEY);
		apiparamsMap.put("timestamp", new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss").format(new Date()));
		apiparamsMap.put("format", "xml");
		apiparamsMap.put("v", "1.0");
		// 组装应用参数
		apiparamsMap.put("fields", "cid,pid,prop_name,vid,name,name_alias,status,sort_order");
		apiparamsMap.put("cid", cid);
		apiparamsMap.put("pvs", "1627207:");
		
		// 获得签名。第二个参数为分配给您的APP_SECRET
		String sign = Util.sign(apiparamsMap,DemoConstant.APP_SERCET);
		// 组装协议参数sign
		apiparamsMap.put("sign", sign);

		StringBuilder param = new StringBuilder();
		for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet()
				.iterator(); it.hasNext();) {
			Map.Entry<String, String> e = it.next();
			param.append("&").append(e.getKey()).append("=").append(
					e.getValue());
		}
		return param.toString().substring(1);
	}

	/**
	 * 调用api，获得结果，组装成java对象
	 * @param cid 商品的类目id
	 * @return 属性值列表
	 * @throws DemoException
	 */
	public static List<PropValue> getPropValue(String cid) throws DemoException {
		//调用api，获得结果
		String result = Util.getResult(DemoConstant.SANDBOX_URL,createRequestParam(cid));
		//解析获得的xml字符串，组装成java对象
		StringReader read = new StringReader(result);
		InputSource source = new InputSource(read);
		SAXBuilder builder = new SAXBuilder();
		List<PropValue> props = new ArrayList<PropValue>();
		try {
			Document document = builder.build(source);
			Element root = document.getRootElement();
			List<Element> childNodes = root.getChildren();
			Element element = null;
			
			PropValue PropValue = null;
			for (int i = 0; i < childNodes.size() ; i++) {
				element = (Element) childNodes.get(i);
				PropValue = new PropValue();
				PropValue.setCid(element.getChildText("cid"));
				PropValue.setPid(element.getChildText("pid"));
				PropValue.setName(element.getChildText("name"));
				PropValue.setNameAlias(element.getChildText("name_alias"));
				PropValue.setStatus(element.getChildText("status"));
				PropValue.setPropName(element.getChildText("prop_name"));
				PropValue.setVid(element.getChildText("vid"));
				props.add(PropValue);
			}
		} catch (JDOMException e) {
			throw new DemoException("返回结果出错!");
		} catch (IOException e) {
			throw new DemoException("返回结果出错!");
		} 
		return props;
	}
}
